<template>
  <div class="plan-container">
    <div class="plan-header">
      <div class="plan-header-main">
        <i class="el-icon-edit-outline"></i>
        <p>已保存</p>
        <span v-if="planInfo">上次保存时间: {{ planInfo.modifyTime }}</span>
        <div class="btn-action">
          <el-button size="medium" :disabled="currentIndex === 0" @click="setCurrent(currentIndex - 1)">上一步</el-button>
          <el-button size="medium" v-if="currentIndex !== 2" @click="nextCurrent">下一步</el-button>
          <el-button size="medium" v-if="currentIndex === 2" @click="release">发布</el-button>
        </div>
      </div>
    </div>
    <div class="plan-content">
      <div class="plan-steps">
        <ul>
          <li
            v-for="(step, idx) in ['编辑计划', '设置课次', '预览/编辑讲义']" 
            :key="step" 
            :class="{ 'is__current': currentIndex === idx, 'is__complete': finished >= idx }"
            :data-index="idx + 1"
          >
            <i class="el-icon-check" />
            <span>{{ step }}</span>
          </li>
        </ul>
      </div>

      <div class="plan-main" v-if="currentIndex === 0"><BasicComponent @basic-finally="basicFinally" /></div>
      <div class="plan-main" v-else-if="currentIndex === 1"><ClassTimesComponent /></div>  
      <div class="plan-preview" v-else><PreviewComponent /></div>
    </div>
  </div>
</template>

<script lang="ts">
import BasicComponent from './basic.vue';
import ClassTimesComponent from './classTimes.vue';
import PreviewComponent from './preview.vue';
import $ from '$';
import { computed, inject, provide } from 'vue';
import store from './store';
import { ElMessage } from 'element-plus';

export default {
  components: { BasicComponent, ClassTimesComponent, PreviewComponent },
  setup() {
    let [ currentIndex, setCurrent ] = $.useState(0);

    let [ finished ] = $.useState(-1);

    let planInfo: any = inject('planInfo');
    store.commit('set_plan_info', planInfo);

    let timesList = computed(() => store.state.timesList);
    const nextCurrent = () => {
      if (currentIndex.value < 1) {
        planInfo && setCurrent(1)
        !planInfo && ElMessage.warning('请保存后点击下一步')
      } else if (timesList.value.every(item => !!item.child)) {
        setCurrent(2)
      } else {
        ElMessage.warning('请为所有课次绑定讲义!');
      }
    }
    let close: any = inject("close");
    const release = () => {
      let params = {
        publishStatus: 1,
        id: planInfo.id
      }
      $.http.post('/admin/teachPlan/saveOrUpdate', params).then(res => {
        if (res.result) {
          ElMessage.success('发布成功')
          close()
        } else {
          ElMessage.success(res.msg)
        }
      })
    }

    const basicFinally = (data) => {
      planInfo = data;
      store.commit('set_plan_info', planInfo);
      setCurrent(1);
    }

    provide('getPlanInfo', () => planInfo);

    return { currentIndex, setCurrent, finished, basicFinally, nextCurrent, release, close, planInfo }
  }
}
</script>

<style lang="scss" scoped>
$mainWidth: 1120px;
.plan-container {
  display: flex;
  height: 100%;
  flex-direction: column;
  background: #F4F5F9;
}
.plan-header {
  height: 60px;
  color: #fff;
  line-height: 60px;
  background: #72b22a;
  box-shadow: 0px 0px 3px 0px rgba(45, 113, 183, .15);
  .plan-header-main {
    display: flex;
    width: $mainWidth;
    margin: 0 auto;
    i {
      font-size: 20px;
      line-height: 60px;
    }
    p {
      margin: 0 20px 0 10px;
    }
    span {
      font-weight: 400;
    }
    .btn-action {
      margin-left: auto;
      .el-button {
        padding: 10px 34px;
        color: #fff;
        border-radius: 18px;
        background: rgba($color: #000000, $alpha: 0);
        &:active {
          border-color: #fff;
        }
        &:last-child {
          color: #72b22a;
          background: #fff;
        }
      }
    }
  }
}
.plan-content {
  display: flex;
  flex-direction: column;
  width: $mainWidth;
  flex: 1 1 60px;
  margin: 20px auto 10px;
  overflow: auto;
  .plan-main {
    padding: 5px 30px 30px;
    background: #fff;
    box-shadow: 0px 1px 6px 0px rgba(91, 125, 255, .08);
    border-radius: 6px;
    border: 1px solid #EBF0FC;
  }
  .plan-preview {
    display: flex;
    flex: 1 1 70px;
    overflow: hidden;
  }
}
.plan-steps {
  padding: 15px 130px;
  margin-bottom: 20px;
  background: #fff;
  border-radius: 6px;
  ul {
    height: 40px;
    display: flex;
    overflow: hidden;
    li {
      flex: 1;
      color: #666;
      font-size: 18px;
      line-height: 40px;
      text-align: center;
      background: #F0F2F5;
      position: relative;
      cursor: pointer;
      &.is__disabled {
        pointer-events: none;
      }
      &.is__complete {
        color: #fff;
        background: #99D6D2;
        i {
          display: inline-block;
        }
        &::before {
          display: none;
        }
      }
      &.is__current {
        color: #fff;
        background: #63C7C2;
      }
      &.is__complete, 
      &.is__current {
        &::before {
          border-color: #fff;
        }
      }
      &:not(:first-child) {
        margin-left: -10px;
      }
      &:not(:last-child)::after {
        content: '';
        display: block;
        width: 32px;
        height: 30px;
        border-top: solid 10px #fff;
        border-right: solid 10px #fff;
        background: inherit;
        position: absolute;
        top: 0;
        right: 0;
        z-index: 2;
        margin-right: -10px;
        border-radius: 6px;
        transform: rotateZ(45deg);
      }
      &:first-child {
        border-top-left-radius: 6px;
        border-bottom-left-radius: 6px;
      }
      &:last-child {
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }
      &::before {
        content: attr(data-index);
        display: inline-block;
        width: 26px;
        height: 26px;
        margin-top: 7px;
        margin-right: 10px;
        line-height: 22px;
        color: inherit;
        font-size: 14px;
        text-align: center;
        border: solid 2px #666;
        border-radius: 50%;
        box-sizing: border-box;
        vertical-align: top;
      }
      i {
        display: none;
        width: 26px;
        height: 26px;
        color: #99D6D2;
        line-height: 26px;
        text-align: center;
        font-weight: bold;
        background: #fff;
        vertical-align: top;
        margin: 7px 10px 0 0;
        border-radius: 50%;
      }
    }
  }
}

</style>